import { OperateBase } from '../../common/operate-base';
import { FreezePlugin } from '../../../pugins/freeze.plugin';


export class FreezeRow extends OperateBase {
    protected container: HTMLElement;
    protected plugin: FreezePlugin
    constructor() {
        super();
    }
    public getDom() {
        this.container = document.createElement('div');
        this.container.setAttribute('class', this.containerClassName);
        this.container.innerHTML=`<span class="z-tools-icon icon iconfont icon-freeze-rows"></span>`;
        return this.container;
    }
    public afterDomInit(): void {
        this.plugin = this.zTableInstance.pluginManager.getPlugin(FreezePlugin.pluginName);
        this.zTableInstance.getHook().afterSelect.add(() => {
            this.toggleDisabled();
        });
        this.toggleDisabled();
        let iconEle = this.container.querySelector('.icon-freeze-rows') as HTMLElement;
        iconEle.addEventListener('click', () => {
            if(iconEle.classList.contains('z-table-icon-disabled')) {
                return;
            }
            if(this.plugin.getFreezeInfo().row !== -1) {
                this.plugin.cancelFreezeRow();
                this.toggleDisabled();
            } else {
                let select = this.plugin.getSelectInfo();
                if(select !== null && select.row !== -1) {
                    this.plugin.freezeRow(select.row);
                    this.toggleDisabled();
                }
            }
        });
    }
    protected toggleDisabled() {
        let select = this.plugin.getSelectInfo();
        let iconEle = this.container.querySelector('.icon-freeze-rows') as HTMLElement;
        if(this.plugin.getFreezeInfo().row !== -1) {
            iconEle.classList.remove('z-table-icon-disabled');
        } else {
            if(select) {
                iconEle.classList.remove('z-table-icon-disabled');
            } else {
                iconEle.classList.add('z-table-icon-disabled');
            }
        }
    }
    public selectChange(select: any, cells: any[]): void {
        
    }
}